package edu.scau.pos215.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.pos215.core.domain.AjaxResult;
import edu.scau.pos215.domain.Category;
import edu.scau.pos215.domain.ProductDescription;
import edu.scau.pos215.service.ICategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @GetMapping("/{categoryId}")
    @ApiOperation("根据id查找category")
    public AjaxResult getById(@PathVariable("categoryId") Long categoryId){

        return AjaxResult.success(categoryService.getCategoryById(categoryId));
    }

    @GetMapping("/list")
    @ApiOperation("查找category")
    public AjaxResult getAllCategory(Category category){
        List<Category> list = categoryService.listCategory(category);
        return AjaxResult.success(list);
    }

    @PostMapping
    @ApiOperation("新增类别")
    public AjaxResult add(@RequestBody Category category) {
        int rows = categoryService.insertCategory(category);
        return rows > 0 ? AjaxResult.success("添加产品成功") : AjaxResult.error("添加产品失败");
    }

    @PutMapping
    @ApiOperation("修改类别")
    public AjaxResult edit(@RequestBody Category category) {
        int rows = categoryService.updateCategory(category);
        return rows > 0 ? AjaxResult.success("修改产品成功") : AjaxResult.error("修改产品失败");
    }

    @DeleteMapping("/{categoryId}")
    @ApiOperation("删除类别")
    public AjaxResult remove(@PathVariable Long categoryId) {
        int rows = categoryService.deleteCategoryById(categoryId);
        return rows > 0 ? AjaxResult.success("删除产品成功") : AjaxResult.error("删除产品失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Category category){
        PageHelper.startPage(pageNum,pageSize);
        List<Category> list = categoryService.listCategory(category);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }

}
